package com.demo.apparel.service.uniapp.Impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.demo.apparel.common.IdGenerator;
import com.demo.apparel.dto.uniapp.GoodsCreateDTO;
import com.demo.apparel.dto.uniapp.GoodsUploadDTO;
import com.demo.apparel.dto.uniapp.GoodsWithAvatarDTO;
import com.demo.apparel.entity.Clothes;
import com.demo.apparel.entity.Goods;
import com.demo.apparel.entity.Shop;
import com.demo.apparel.exception.BusinessException;
import com.demo.apparel.mapper.uniapp.GoodsMapper;
import com.demo.apparel.mapper.uniapp.ShopMapper;
import com.demo.apparel.mapper.uniapp.WxClothesMapper;
import com.demo.apparel.mapper.uniapp.WxUserMapper;
import com.demo.apparel.service.uniapp.WxGoodsService;
import com.demo.apparel.vo.PageVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class WxGoodsServiceImpl implements WxGoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    private ObjectMapper objectMapper = new ObjectMapper();

    @Autowired
    private WxClothesMapper wxClothesMapper;

    @Autowired
    private WxUserMapper wxuserMapper;

    @Autowired
    private ShopMapper shopMapper;

//    @Override
//    @Transactional
//    public void createGoods(GoodsUploadDTO dto, String userId) {
//        Goods goods = new Goods();
//        goods.setId(IdGenerator.generateGoodsId());
//        goods.setClothesId(dto.getClothesId());
//        goods.setUserId(userId);
//        goods.setCategory(dto.getCategory());
//        goods.setName(dto.getName());
//        goods.setDescription(dto.getDescription());
//        goods.setIntroduce(dto.getIntroduce());
//        goods.setPrice(dto.getPrice());
//        goods.setImageUrls(JSONUtil.toJsonStr(dto.getImageUrls()));
//        goods.setSize(dto.getSize());
//        goods.setSeason(dto.getSeason());
//        goods.setStatusSale(dto.getStatusSale());
//        goods.setCreatedTime(LocalDateTime.now());
//        goods.setUpdatedTime(LocalDateTime.now());
//        goodsMapper.insertGoods(goods);
//
//        wxClothesMapper.update(
//                null,
//                new UpdateWrapper<Clothes>()
//                        .set("status_sale", dto.getStatusSale())
//                        .eq("id", dto.getClothesId())
//        );
//    }

    @Override
    @Transactional
    public void createGoods(GoodsUploadDTO dto, String userId) {
        // 1. 根据 userId 查询商家店铺
        Shop shop = shopMapper.selectOne(
                new QueryWrapper<Shop>().eq("user_id", userId)
        );

        if (shop == null) {
            throw new BusinessException(400,"你未申请商家入驻，不能发布商品！");
        }

        // 2. 创建商品
        Goods goods = new Goods();
        goods.setId(IdGenerator.generateGoodsId());
        goods.setClothesId(dto.getClothesId());
        goods.setUserId(userId);
        goods.setShopId(shop.getShopId());
        goods.setCategory(dto.getCategory());
        goods.setName(dto.getName());
        goods.setDescription(dto.getDescription());
        goods.setIntroduce(dto.getIntroduce());
        goods.setPrice(dto.getPrice());
        goods.setImageUrls(JSONUtil.toJsonStr(dto.getImageUrls()));
        goods.setSize(dto.getSize());
        goods.setSeason(dto.getSeason());
        goods.setStatusSale(dto.getStatusSale());
        goods.setCreatedTime(LocalDateTime.now());
        goods.setUpdatedTime(LocalDateTime.now());

        goodsMapper.insertGoods(goods);

        // 3. 同步更新衣物表状态
        wxClothesMapper.update(
                null,
                new UpdateWrapper<Clothes>()
                        .set("status_sale", dto.getStatusSale())
                        .eq("id", dto.getClothesId())
        );
    }


    @Override
    public PageVO<GoodsWithAvatarDTO> getGoodsPage(int pageNum, int pageSize, String name, String category) {
        int offset = (pageNum - 1) * pageSize;
        List<GoodsWithAvatarDTO> records = goodsMapper.selectGoodsPage(offset, pageSize, name, category);
        long total = goodsMapper.countGoods(name, category);
        return new PageVO<>(records, total, pageNum, pageSize);
    }



    @Override
    public GoodsCreateDTO getGoodsDetailById(String goodsId) {
        Goods goods = goodsMapper.selectById(goodsId);
        if (goods == null) {
            throw new RuntimeException("商品不存在");
        }

        // 获取用户头像
        String userAvatar = wxuserMapper.selectById(goods.getUserId()).getAvatar();

        // 转换为 DTO
        GoodsCreateDTO dto = new GoodsCreateDTO();
        dto.setId(goods.getId());
        dto.setClothesId(goods.getClothesId());
        dto.setUserId(goods.getUserId());
        dto.setCategory(goods.getCategory());
        dto.setName(goods.getName());
        dto.setPrice(goods.getPrice());
        dto.setDescription(goods.getDescription());
        dto.setIntroduce(goods.getIntroduce());
        dto.setSize(goods.getSize());
        dto.setSeason(goods.getSeason());
        dto.setStatusSale(goods.getStatusSale());
        dto.setUserAvatar(userAvatar); // 设置用户头像

//        String imageUrlsStr = goods.getImageUrls();

        List<String> imageUrlsList = JSONUtil.toList(goods.getImageUrls(), String.class);
        dto.setImageUrls(imageUrlsList);

        dto.setCreatedTime(String.valueOf(goods.getCreatedTime()));
        dto.setUpdatedTime(String.valueOf(goods.getUpdatedTime()));
        return dto;
    }




}
